﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Entity;
using PlasmaMes.A_Models;

namespace PlasmaMes.Services
{
    public  class Rfid
    {

        private static Rfid instance;

        private static readonly object locker = new object();


        private Rfid() { }


        public static Rfid Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        instance = new Rfid();
                    }
                }
                return instance;
            }
        }

        public  EnService enService;

        public volatile bool isConnect = false;

        public volatile bool isUseGPO1 = false;     
        public volatile bool isUseGPO2 = false;
        public volatile bool isUseGPO3 = false;
        public volatile bool isUseGPO4 = false;

        public volatile bool isEnableRF1 = false;
        public volatile bool isEnableRF2 = false;
        public volatile bool isEnableRF3 = false;
        public volatile bool isEnableRF4 = false;

        public GPIO GPIN = new GPIO();
        // RFO
        public ReaderMonitor reader0 = new ReaderMonitor();
        // RF1
        public ReaderMonitor reader1 = new ReaderMonitor();
        // RF2
        public ReaderMonitor reader2 = new ReaderMonitor();
        // RF3
        public ReaderMonitor reader3 = new ReaderMonitor();


        public bool EnServiceConnect(string ip,  string port)
        {
            if(enService == null)
            {
                enService = new EnService(ip, port);
                enService.DeviceMonitor += EnService_DeviceMonitor;
            }

            if (enService.Connect())
            {
                isConnect = true;
                LogService.Instance.LogInfo("EnService 对象 连接网关成功");

                //if (Rfid.Instance.enService.SetOutBit(new Dictionary<GPOS, bool>() { [GPOS.GPO_1] = true }))
                //{
                //    LogService.Instance.LogInfo("启用GPO1输出 成功");
                //    RunParam.Instance.IsUseGPO1 = true;
                //}
                //else
                //{
                //    LogService.Instance.LogError("启用GPO1输出 失败");
                //}

                return true;
            }
            else
            {
                LogService.Instance.LogInfo("EnService 对象 连接网关失败");
                return false;
            }
        }

        public void EnServiceDisConnect()
        {
            enService.DisConnect();  
            isConnect = false;
        }

        private void EnService_DeviceMonitor(Dictionary<Reader, Entity.ReaderMonitor> ReaderInfos, Entity.GPIO IOInfos)
        {
            GPIN = IOInfos;
            reader0 = ReaderInfos[Reader.RF0];
            reader1 = ReaderInfos[Reader.RF1];
            reader2 = ReaderInfos[Reader.RF2];
            reader3 = ReaderInfos[Reader.RF3];
        }


        public  string ToHexString( byte[] byteArray)
        {
            if (byteArray == null || byteArray.Length == 0)
                return string.Empty;

            return string.Join(" ", byteArray.Select(b => b.ToString("X2")));
        }


        public void ReadRfidCode(string rfid_addr, string rfid_length)
        {
            int Addr = int.Parse(rfid_addr);
            int Count = int.Parse(rfid_length);

            //默认使用Rf1
            ReadResult result = Rfid.Instance.enService.ReadTagUsers(Reader.RF0, Addr, Count);

            if (result.sataus == ExecutSataus.Cmd_Succeed)
            {
                string data = "";
                StringBuilder str = new StringBuilder();
                foreach (var item in result.TagUser.ByteArray)
                {
                    str.Append(item.ToString() + " ");
                }

                data = str.ToString();

                

            }
            else
            {
               


            }
        }










    }
}
